﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class VendorReconciliationRequestService : BaseService, IVendorReconciliationRequestService
    {
        private readonly IVendorReconciliationRequestRepository _vendorReconciliationRequest;
        private readonly IUnitOfWork unitOfWork;
        public VendorReconciliationRequestService(IMapper mapper, IVendorReconciliationRequestRepository vendorReconciliationRequest, IUnitOfWork unitOfWork) : base(mapper)
        {
            _vendorReconciliationRequest = vendorReconciliationRequest;
            this.unitOfWork = unitOfWork;
        }

        public bool AddOrModefiyVendorReconciliationRequest(VendorReconciliationRequest model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _vendorReconciliationRequest.UpdEntity(model);
            }
            else
            {
                _vendorReconciliationRequest.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }

        public bool DelVendorReconciliationRequest(long vendorReconciliationRequestId)
        {
            var Quote = _vendorReconciliationRequest.QueryEntityById(vendorReconciliationRequestId);
            if (Quote == null)
            {
                return false;
            }
            _vendorReconciliationRequest.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public VendorReconciliationRequest? GetVendorReconciliationRequest(long vendorReconciliationRequestId)
        {
            return _vendorReconciliationRequest.QueryEntityById(vendorReconciliationRequestId);
        }

        public List<VendorReconciliationRequest> GetVendorReconciliationRequestData(out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _vendorReconciliationRequest.GetEntityAlls();
           
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
    }
}
